<?php
/**
 * Created by PhpStorm.
 * User: testypp
 * Date: 2017/12/18
 * Time: 下午2:18
 */


class Model_User extends PhalApi_Model_NotORM {

    // 根据userID获取User
    public function getUserByID ($userId) {
        return $this->getORM()
            ->select('*')
            ->where('id = ?', $userId)
            ->fetch();
    }

    // 根据Mobile获取User
    public function getUserByMobile ($mobile) {
        return $this->getORM()
            ->select('*')
            ->where('mobile = ?', $mobile)
            ->fetch();
    }


    public function addUser ($mobile, $password, $nickname) {

        // 生成当前时间
        $date = date("Y-m-d H:i:s");

        // 先检查有无注册过
        $tempResult = $this->getORM()
                        ->select('*')
                        ->where('mobile = ?', $mobile)->count();

        if ($tempResult == 0) {
            $data = array('mobile' => $mobile, 'password' => $password, 'status' => 1, 'createtime' => $date, 'nickname' => $nickname);
            return $this->getORM()->insert($data);
        } else {
            return null;
        }

    }

    public function loginUser ($mobile, $password) {

        return $this->getORM()
            ->select('*')
            ->where('mobile = ?', $mobile)
            ->where('password = ?', $password)
            ->fetch();
    }



}